﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKMaterialStatistic;

namespace YJKStatisticTable.FastStatisticTable.Utility
{
	internal class RoofsGrid
	{
		public RoofsGrid(Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan)
		{
			this.m_lstViewPlan = lstViewPlan;
			if (!this.StatisticData())
			{
				return false;
			}
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		public bool GetStatisticGrid(List<Element> lstSelElems)
		{
			this.m_lstSelElems = lstSelElems;
			this.StatisticDataInSelArea();
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private bool StatisticData()
		{
			if (this.m_doc == null)
			{
				return false;
			}
			try
			{
				ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计主文档中屋顶的信息", ProgressBarStyle.Blocks, true, false);
				progressBarForm.Show(WindowHelper.HWndRevit);
				int num = 0;
				foreach (ViewPlan viewPlan in this.m_lstViewPlan)
				{
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						return false;
					}
					ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
					Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
					transaction.Commit();
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc, viewPlan.Id);
					IList<Element> list = filteredElementCollector.OfCategory(BuiltInCategory.OST_Roofs).ToElements();
					foreach (Element element in list)
					{
						if (!this.m_dicData.ContainsKey(element.Id))
						{
							RoofType roofType = this.m_doc.GetElement(element.GetTypeId()) as RoofType;
							if (roofType != null)
							{
								num++;
								List<string> list2 = new List<string>();
								list2.Insert(0, "");
								list2.Insert(1, "屋" + num.ToString());
								list2.Insert(2, roofType.Name);
								list2.Insert(3, "");
								list2.Insert(4, "");
								this.m_dicData.Add(element.Id, list2);
							}
						}
					}
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(elementId);
					transaction.Commit();
				}
				if (this.m_gridSet.LinkFile)
				{
					List<RevitLinkInstance> allRevitLinkInstancesInDoc = this.GetAllRevitLinkInstancesInDoc();
					progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count * allRevitLinkInstancesInDoc.Count, "统计链接文档中屋面的信息", ProgressBarStyle.Blocks, true, false);
					progressBarForm.Show(WindowHelper.HWndRevit);
					foreach (RevitLinkInstance revitLinkInstance in allRevitLinkInstancesInDoc)
					{
						Document linkDocument = revitLinkInstance.GetLinkDocument();
						foreach (ViewPlan viewPlan2 in this.m_lstViewPlan)
						{
							progressBarForm.RefreshProgressBar();
							if (progressBarForm.m_stopTag)
							{
								return false;
							}
							ElementId elementId2 = viewPlan2.GetUnderLayParameter().AsElementId();
							Transaction transaction2 = new Transaction(this.m_doc, "ChangeBaseLine");
							transaction2.Start();
							viewPlan2.GetUnderLayParameter().Set(ElementId.InvalidElementId);
							transaction2.Commit();
							FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(linkDocument, viewPlan2.Id);
							IList<Element> list3 = filteredElementCollector2.OfCategory(BuiltInCategory.OST_Roofs).ToElements();
							foreach (Element element2 in list3)
							{
								if (!this.m_dicData.ContainsKey(element2.Id))
								{
									RoofType roofType2 = this.m_doc.GetElement(element2.GetTypeId()) as RoofType;
									if (roofType2 != null)
									{
										num++;
										List<string> list4 = new List<string>();
										list4.Insert(0, "");
										list4.Insert(1, "屋" + num.ToString());
										list4.Insert(2, roofType2.Name);
										list4.Insert(3, "");
										list4.Insert(4, "");
										this.m_dicData.Add(element2.Id, list4);
									}
								}
							}
							transaction2.Start();
							viewPlan2.GetUnderLayParameter().Set(elementId2);
							transaction2.Commit();
						}
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
				return false;
			}
			return true;
		}

		private void StatisticDataInSelArea()
		{
			try
			{
				ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstSelElems.Count, "统计区域中屋面的信息", ProgressBarStyle.Blocks, true, false);
				progressBarForm.Show(WindowHelper.HWndRevit);
				int num = 0;
				foreach (Element element in this.m_lstSelElems)
				{
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						break;
					}
					if (!this.m_dicData.ContainsKey(element.Id) && element.Category.Id.IntegerValue == -2000035)
					{
						RoofType roofType = this.m_doc.GetElement(element.GetTypeId()) as RoofType;
						if (roofType != null)
						{
							num++;
							List<string> list = new List<string>();
							list.Insert(0, "");
							list.Insert(1, "屋" + num.ToString());
							list.Insert(2, roofType.Name);
							list.Insert(3, "");
							list.Insert(4, "");
							this.m_dicData.Add(element.Id, list);
						}
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private List<RevitLinkInstance> GetAllRevitLinkInstancesInDoc()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc).OfClass(typeof(RevitLinkInstance));
			ICollection<ElementId> collection = filteredElementCollector.ToElementIds();
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			foreach (ElementId elementId in collection)
			{
				Element element = this.m_doc.GetElement(elementId);
				if (!(element.GetType() != typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
					if (revitLinkInstance != null)
					{
						list.Add(revitLinkInstance);
					}
				}
			}
			return list;
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.TitleFontSize;
			int num = 2;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			Dictionary<ElementId, List<string>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int count = this.m_dicData.Count;
			if (count == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + count + 1;
			foreach (KeyValuePair<ElementId, List<string>> keyValuePair in this.m_dicData)
			{
				List<string> value = keyValuePair.Value;
				num++;
				for (int i = 1; i < value.Count; i++)
				{
					this.m_grid.Cell(num, i).Text = value[i];
					this.m_grid.Cell(num, i).Alignment = AlignmentEnum.CenterCenter;
				}
				this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).Sort(1, SortTypeEnum.Text, SortEnum.Ascending);
			}
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			if (this.m_gridSet.SetColWidth)
			{
				this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			}
			else
			{
				this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = false;
			}
			for (int j = 1; j < this.m_grid.Rows; j++)
			{
				this.m_grid.Row(j).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<ElementId, List<string>> m_dicData = new Dictionary<ElementId, List<string>>();

		private List<ViewPlan> m_lstViewPlan;

		private List<Element> m_lstSelElems;
	}
}
